using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.Security;
using Deepcode.Flux.Core.Systems.Audit;

public partial class Private_Config_Event_search : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxGeneralAdmin"; } }
	#endregion
	#region Viewstate properties
	private SearchEventType ListType
	{
		get
		{
			object o = ViewState["evType"];
			if (o == null) return SearchEventType.All;
			return (SearchEventType)o;
		}
		set
		{
			ViewState["evType"] = value;
		}
	}
	private EventSeverity ListSeverityLevel
	{
		get
		{
			object o = ViewState["evLevel"];
			if (o == null) return EventSeverity.Trace;
			return (EventSeverity)o;
		}
		set
		{
			ViewState["evLevel"] = value;
		}
	}
	private string Criteria
	{
		get
		{
			string o = (string)ViewState["criteria"];
			if (o == null) return String.Empty;
			return o;
		}
		set
		{
			ViewState["criteria"] = value;
		}
	}
	private int UserID
	{
		get
		{
			object o = ViewState["uid"];
			if (o == null) return 0;
			return (int)o;
		}
		set
		{
			ViewState["uid"] = value;
		}
	}
	private DateTime StartDate
	{
		get
		{
			object o = ViewState["sdate"];
			if (o == null) return new System.DateTime(1990, 1, 1, 0, 0, 0);
			return (DateTime)o;
		}
		set
		{
			ViewState["sdate"] = value;
		}
	}
	private DateTime EndDate
	{
		get
		{
			object o = ViewState["edate"];
			if (o == null) return System.DateTime.Now;
			return (DateTime)o;
		}
		set
		{
			ViewState["edate"] = value;
		}
	}
	private string ListClass
	{
		get
		{
			string o = (string)ViewState["evClass"];
			if (o == null) return String.Empty;
			return o;
		}
		set
		{
			ViewState["evClass"] = value;
		}
	}
	private string ListApp
	{
		get
		{
			string o = (string)ViewState["evApp"];
			if (o == null) return String.Empty;
			return o;
		}
		set
		{
			ViewState["evApp"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			string tmpEventType = GetAttribute("type", "");
			string tmpSeverityType = GetAttribute("severity", "");

			// Decide what type's we're looking for
			if (tmpEventType == "") ListType = SearchEventType.All;
			if (tmpEventType == "System") ListType = SearchEventType.System;
			if (tmpEventType == "Security") ListType = SearchEventType.Security;
			if (tmpEventType == "Application") ListType = SearchEventType.Application;
			// Decide what severity level to report from
			if (tmpSeverityType == "Trace") ListSeverityLevel = EventSeverity.Trace;
			if (tmpSeverityType == "Information") ListSeverityLevel = EventSeverity.Information;
			if (tmpSeverityType == "Important") ListSeverityLevel = EventSeverity.Important;
			if (tmpSeverityType == "Error") ListSeverityLevel = EventSeverity.Error;
			if (tmpSeverityType == "Critical") ListSeverityLevel = EventSeverity.Critical;

			ListClass = GetAttribute("class", "");
			ListApp = GetAttribute("application", "");
			Criteria = GetAttribute("criteria", "");
			UserID = GetAttribute("uid", 0);
			try { StartDate = System.DateTime.Parse(GetAttribute("startdate", System.DateTime.Now.ToString())); }
			catch { StartDate = new System.DateTime(1990, 1, 1); }
			try { EndDate = System.DateTime.Parse(GetAttribute("enddate", ((new System.DateTime(1990, 1, 1, 0, 0, 0)).ToString()))); }
			catch { EndDate = System.DateTime.Now.AddDays(1); }

			// Now obtain the information to list - provided we have SOME criteria
			if (!((tmpEventType == "") && (ListClass == "") && (ListApp == ""))) BindReport();
		}
	}
	#endregion
	#region Data Binding
	/// <summary>
	/// Query the event log and display in the grid
	/// </summary>
	private void BindReport()
	{
		SystemEvents ev = new SystemEvents();
		_data.DataSource = ev.SearchEvents(
						this.Criteria,
						this.UserID,
						this.ListType,
						this.ListSeverityLevel,
						this.ListClass,
						this.ListApp,
						StartDate,
						EndDate);

		_data.DataBind();
	}
	#endregion
	#region Custom databinding expressions
	/// <summary>
	/// Get the user's name and email address
	/// </summary>
	/// <param name="item"></param>
	/// <returns></returns>
	protected string BindingForUser(object item)
	{
		if( item is SystemEvent )
		{
			SystemEvent ev = ((SystemEvent) item);
			AdministrationUsers users = new AdministrationUsers();
			AdministrationUser user = users.GetUserByID( ev.FK_User );

			if (user == null)
			{
				if( ev.FK_User == -1 ) return "System user";
				return "Unknown (ID:" + ev.FK_User + ")";
			}
			return user.EMailAddress + "/" + user.RealName;
		}
		return "???";
	}
	/// <summary>
	/// Get the textual representation of the severity enum
	/// </summary>
	/// <param name="item"></param>
	/// <returns></returns>
	protected string BindingForSeverity(object item)
	{
		if( item is SystemEvent )
		{
			SystemEvent ev = ((SystemEvent) item);
			return ev.Severity.ToString();
		}
		return "???";
	}
	/// <summary>
	/// Get the appropriate icon image depending upon the severity
	/// </summary>
	/// <param name="item"></param>
	/// <returns></returns>
	protected string BindingForSeverityImage(object item)
	{
		string file = "~/admin/Asset/Image/shim.gif";
		if( item is SystemEvent )
		{
			SystemEvent ev = ((SystemEvent) item);
			switch( ev.Severity )
			{
				case EventSeverity.Critical:
					file = "~/admin/private/config/event/image/critical.gif";
					break;
				case EventSeverity.Error:
					file = "~/admin/private/config/event/image/error.gif";
					break;
				case EventSeverity.Important:
					file = "~/admin/private/config/event/image/important.gif";
					break;
				case EventSeverity.Information:
					file = "~/admin/private/config/event/image/information.gif";
					break;
				case EventSeverity.Trace:
					file = "~/admin/private/config/event/image/trace.gif";
					break;
			}
		}
		return Page.ResolveUrl(file);
	}
	/// <summary>
	/// Get the textual representation of the event type enum
	/// </summary>
	/// <param name="item"></param>
	/// <returns></returns>
	protected string BindingForType(object item)
	{
		if( item is SystemEvent )
		{
			SystemEvent ev = ((SystemEvent) item);
			return ev.Type.ToString();
		}
		return "???";
	}
	#endregion
}
